--
-- Description: Script to save an event hit record.
-- Steps include creating a new record in the event hits table.
-- Date created: 6 May 2008
--

set ANSI_NULLS on
set QUOTED_IDENTIFIER on
go

--
-- drop the stored procedure if it exists and recreate it.
--
if exists
(
	select * 
	from sys.objects
	where object_id = object_id(N'[saveEventHit_sp]')
	and type in (N'P', N'PC')
)
begin
    drop procedure [dbo].[saveEventHit_sp]
end
go

create procedure [dbo].[saveEventHit_sp]
(
	@EventID bigint
	, @EventTypeName nvarchar(50)
	, @DateCreated smalldatetime
	, @DateModified smalldatetime
	, @TotalHits bigint
    , @OperatingSystemName nvarchar(100)
	, @OperatingSystemVersion varchar(100)
    , @LocaleID smallint
    , @LocaleCode varchar(15)
	, @LocaleName nvarchar(400)
)
as
begin
	declare @OperatingSystemID smallint
			, @EventTypeID smallint
	--
	-- save the event type information if required
	--
	exec @EventTypeID = [dbo].[saveEventType_sp]
				@EventTypeName = @EventTypeName

	--
	-- get the OperatingSystemID from the operating system name and version.
	--
	exec @OperatingSystemID = [dbo].[SaveOperatingSystem_sp] 
				@OperatingSystemName = @OperatingSystemName
				, @OperatingSystemVersion = @OperatingSystemVersion
	
	--
	-- save the locale information into the Locales table
	--
	exec @LocaleID = [dbo].[saveLocale_sp]
				@LocaleID = @LocaleID
				, @LocaleCode = @LocaleCode
				, @LocaleName = @LocaleName

	--
	-- create a new record in the staging EventHits table.
	--
	insert into [dbo].[StageEventHits]
	(
		[EventID]
		, [EventTypeID]
		, [DateCreated]
		, [DateModified]
		, [TotalHits]
		, [OperatingSystemID]
		, [LocaleID]
	)
	values
	(
		@EventID
		, @EventTypeID
		, @DateCreated
		, @DateModified
		, @TotalHits
		, @OperatingSystemID
		, @LocaleID
	)
end
go

set ansi_nulls off
go
set quoted_identifier off
go
